package quickConnect;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JTable;

import org.quickconnect.ControlObject;
import beans.SessionBean;
import beans.UserBean;

import userInterface.ChatSessionPanel;
import userInterface.MainFrame;

public class ChangeSessionMuteBCO implements ControlObject {

	@Override
	public Object handleIt(ArrayList<Object> arg0) {

		HashMap params = (HashMap) arg0.get(0);
		Boolean roomMuted = (Boolean)params.get("roomMuted");
		String id = (String) params.get("sessionId");
		ChatSessionPanel chatSessionPanel;

		ArrayList<ChatSessionPanel> sessions = MainFrame.mainFrame.getChatSessions();
		for(int i = 0; i < sessions.size(); i++){
			SessionBean curSession = sessions.get(i).getSession();
			if(curSession.getSessionId().equals(id)){
				chatSessionPanel = sessions.get(i);
				curSession.setSessionMuted(roomMuted.booleanValue());
				JTable table = chatSessionPanel.getaUserPanel().getTable();
				if(params.containsKey("roomMuted") && roomMuted)
				{
					for(int j = 0; j != table.getRowCount(); j++)
					{
						if(table.getModel().getValueAt(j, 0).equals(MainFrame.mainFrame.getController().getCurUser().getUsername()))
						{
							if(!table.getModel().getValueAt(j, 1).equals("USER"))
							{
								chatSessionPanel.getaMessengerPanel().getSendButton().setForeground(Color.BLACK);
								chatSessionPanel.getaMessengerPanel().getSendButton().setEnabled(true);							
							}
							else
							{
								chatSessionPanel.getaMessengerPanel().getSendButton().setForeground(Color.LIGHT_GRAY);
								chatSessionPanel.getaMessengerPanel().getSendButton().setEnabled(false);							
							}
						}
					}
					MainFrame.mainFrame.setMuteText("Unmute");
				}
				else
				{
					chatSessionPanel.getaMessengerPanel().getSendButton().setForeground(Color.BLACK);
					chatSessionPanel.getaMessengerPanel().getSendButton().setEnabled(true);							
					MainFrame.mainFrame.setMuteText("Mute");
				}
				break;
			}
		}		
		return null;
	}
}
